package com.wgts.wgts.mvp.view.activity.awardmore;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.dialog.WonderLoadingDialog;
import com.wgts.wgts.control.eventbus.OrderFragmentEvenBus;
import com.wgts.wgts.control.extrusionecycleview.ExtrusionRecyclerView;
import com.wgts.wgts.control.extrusionecycleview.PowerfulStickyDecoration;
import com.wgts.wgts.control.extrusionecycleview.listener.PowerGroupListener;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAwardMoreFragmentComponent;
import com.wgts.wgts.dagger.module.GuessingAndAwardFragmentModule;
import com.wgts.wgts.mvp.presenter.IGuessingAndAwardFragmentPresenter;
import com.wgts.wgts.mvp.view.IGuessingAndAwardFragmentView;
import com.wgts.wgts.mvp.view.activity.guessing.GuessingAndAwardDetailsActivity;
import com.wgts.wgts.mvp.view.activity.moremore.MoreMoreAffirmOrderActivity;
import com.wgts.wgts.netmodle.resp.AwardPrizeEntity;
import com.wgts.wgts.netmodle.resp.GuessingAndAwardOrderEntity;
import com.wgts.wgts.tools.AppInfor;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import q.rorbin.badgeview.DisplayUtil;

/*
 *
 *
 *   data:2018/10/1311:10
 *
 */public class AwardMoreFragment extends LazyBaseFragment implements IGuessingAndAwardFragmentView {
    private static final String AwardMoreFragment = "awardMoreFragment";
    @BindView(R.id.my_guessing_recycle)
    ExtrusionRecyclerView myGuessingRecycle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.my_join_guessing)
    LinearLayout myJoinGuessing;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;
    private String type;
    private List<GuessingAndAwardOrderEntity> orderList;
    List<AwardPrizeEntity> list;
    private MyAwardOrderParentAdapter orderAdapter;
    private MyMoreMoreAwardAdapter awardAdapter;
    private int dwAllType = 0;

    private WonderLoadingDialog dialog;

    @Inject
    IGuessingAndAwardFragmentPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(AwardMoreFragment);
        }
        View view = inflater.inflate(R.layout.my_guessing_fragment, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    protected void loadData() {
        if (dialog != null){
            dialog.show();
        }
//        if (type.equals("订单")) {
//            if (AppInfor.getInstance().getUserState() > 1) {
//                presenter.initOrderList(dwAllType);
//            } else {
//                presenter.initJoinActivityList(dwAllType);
//            }
//
//        } else {
//            presenter.awardPrizeList(dwAllType);
//        }


        if (type.equals("订单")) {

            if (AppInfor.getInstance().getUserState() > 1) {
                presenter.initOrderList(dwAllType);
            } else {
                presenter.initJoinActivityList(dwAllType);
            }

        } else {

            presenter.awardPrizeList(dwAllType);
            myJoinGuessing.setVisibility(View.GONE);
        }

    }

    @Override
    public void init() {
        DaggerAwardMoreFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .guessingAndAwardFragmentModule(new GuessingAndAwardFragmentModule(this))
                .build()
                .inject(this);
        orderList = new ArrayList<>();
        list = new ArrayList<>();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        dialog = new WonderLoadingDialog(getActivity());
        myJoinGuessing.setBackgroundResource(R.drawable.award_join_bg);
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setEnableRefresh(true);

        if (type.equals("订单")) {
            initOrderRecycle();
            if (AppInfor.getInstance().getUserState() > 1) {
                myJoinGuessing.setVisibility(View.VISIBLE);
            } else {
                myJoinGuessing.setVisibility(View.GONE);
            }
        } else {
            initAwardRecycle();

        }
        load();
    }

    private void initAwardRecycle() {
        //奖品recycle
        myGuessingRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        awardAdapter = new MyMoreMoreAwardAdapter(getActivity(), list);
        myGuessingRecycle.setAdapter(awardAdapter);

        awardAdapter.setOnClickListener(new MyMoreMoreAwardAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int i) {

                switch (list.get(i).status) {
                    case "3":
                        Intent intent = new Intent(getContext(), MoreMoreAffirmOrderActivity.class);
                        intent.putExtra(MoreMoreAffirmOrderActivity.WINNID, list.get(i).winningId);
                        intent.putExtra(MoreMoreAffirmOrderActivity.TYPE, MoreMoreAffirmOrderActivity.MORE_MORE);
                        startActivity(intent);
                        break;
                    case "4":
                        Intent intent1 = new Intent(getContext(), GuessingAndAwardDetailsActivity.class);
                        intent1.putExtra(GuessingAndAwardDetailsActivity.WINNID, list.get(i).winningId);
                        intent1.putExtra(GuessingAndAwardDetailsActivity.TYPE, GuessingAndAwardDetailsActivity.MORE_MORE);
                        startActivity(intent1);
                        break;
                    case "5":
                        Intent intent2 = new Intent(getContext(), GuessingAndAwardDetailsActivity.class);
                        intent2.putExtra(GuessingAndAwardDetailsActivity.WINNID, list.get(i).winningId);
                        intent2.putExtra(GuessingAndAwardDetailsActivity.TYPE, GuessingAndAwardDetailsActivity.MORE_MORE);
                        startActivity(intent2);
                        break;
                }


            }
        });
    }

    private void initOrderRecycle() {
        //订单recycleView
        myGuessingRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        PowerGroupListener listener = new PowerGroupListener() {
            @Override
            public String getGroupName(int position) {
                if (orderList.size() > position){
                    return orderList.get(position).date;
                }
                return null;
            }

            @Override
            public View getGroupView(int position) {
                //获取自定定义的组View
                View view = getLayoutInflater().inflate(R.layout.extrusion_item_head, null, false);
                ((TextView) view.findViewById(R.id.extrusion_head_text)).setText(orderList.get(position).date);
                return view;
            }
        };
        PowerfulStickyDecoration decoration = PowerfulStickyDecoration.Builder
                .init(listener)
                .setGroupBackground(ContextCompat.getColor(getActivity(), R.color.award_bg))
                .setGroupHeight(DisplayUtil.dp2px(getActivity(), 44))
                //重置span（注意：使用GridLayoutManager时必须调用）
                //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
                .build();
        myGuessingRecycle.addItemDecoration(decoration);
        orderAdapter = new MyAwardOrderParentAdapter(getActivity(), orderList, 1, 0);
        myGuessingRecycle.setAdapter(orderAdapter);
    }

    public static Fragment getAwardMoreFragment(String s) {
        Fragment fragment = new AwardMoreFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AwardMoreFragment, s);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Override
    public void onError(int status, String msg) {
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
        noneDataLy.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
    }

    @Override
    public void initOrderList(List<GuessingAndAwardOrderEntity> orderEntities, boolean noMore) {
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
        refreshLayout.finishRefresh();
        if (orderEntities.size() > 0){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
        orderList.clear();
        orderList.addAll(orderEntities);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        orderAdapter.setNoMore(noMore);

        orderAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreOrderList(List<GuessingAndAwardOrderEntity> orderEntities, boolean noMore) {
        refreshLayout.finishLoadmore();
        orderList.addAll(orderEntities);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        orderAdapter.setNoMore(noMore);
        orderAdapter.notifyDataSetChanged();
    }

    @Override
    public void initAwardList(List<AwardPrizeEntity> entities, boolean noMore) {
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
        refreshLayout.finishRefresh();
        list.clear();
        list.addAll(entities);
        if (entities.size() > 0){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        awardAdapter.setNoMore(noMore);
        awardAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreAwardList(List<AwardPrizeEntity> entities, boolean noMore) {
        list.addAll(entities);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        refreshLayout.finishLoadmore();
        awardAdapter.setNoMore(noMore);
        awardAdapter.notifyDataSetChanged();
    }

    @Override
    public void initActionList(List<GuessingAndAwardOrderEntity> entities, boolean noMore) {
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
        if (entities.size() > 0){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
        refreshLayout.finishRefresh();
        orderList.clear();
        orderList.addAll(entities);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        orderAdapter.setNoMore(noMore);

        orderAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreActionList(List<GuessingAndAwardOrderEntity> entities, boolean noMore) {
        refreshLayout.finishLoadmore();
        orderList.addAll(entities);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        orderAdapter.setNoMore(noMore);
        orderAdapter.notifyDataSetChanged();
    }

    private void load() {


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (type.equals("订单")) {
                    if (AppInfor.getInstance().getUserState() > 1) {
                        presenter.loadMore(dwAllType);
                    } else {
                        presenter.loadMoreJoinActivityList();
                    }

                } else {
                    presenter.moreAwardPrizeList();
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (type.equals("订单")) {

                    if (AppInfor.getInstance().getUserState() > 1) {
                        presenter.initOrderList(dwAllType);
                    } else {
                        presenter.initJoinActivityList(dwAllType);
                    }

                } else {
                    presenter.awardPrizeList(dwAllType);
                }
            }
        });
    }

    @OnClick({R.id.my_join_guessing, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_join_guessing:
                Intent intent = new Intent(getActivity(), AwardJoinActionActivity.class);
                intent.putExtra(AwardJoinActionActivity.TYPE, AwardJoinActionActivity.MORE_MORE);
                startActivity(intent);
                break;
            case R.id.net_error_ly:
                if (type.equals("订单")) {
                    if (AppInfor.getInstance().getUserState() > 1) {
                        presenter.initOrderList(dwAllType);
                    } else {
                        presenter.initJoinActivityList(dwAllType);
                    }

                } else {
                    presenter.awardPrizeList(dwAllType);
                }
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderFragmentEvenBus event){
        //initOrderRecycle();
        if (type.equals("订单")) {
            if (AppInfor.getInstance().getUserState() > 1) {
                presenter.initOrderList(dwAllType);
            } else {
                presenter.initJoinActivityList(dwAllType);
            }

        } else {
            presenter.awardPrizeList(dwAllType);
        }
    }

}

